#include "worker.h"
void * thread_func(void * arg){
    /* const char perfix[]="/root/wangpan/thread_pool/server/files"; */
    thread_pool_info_t * thread_pool_info=(thread_pool_info_t*)arg;
    while(1){
        // get netfd old_netfd
        pthread_mutex_lock(&thread_pool_info->mutex);
        while(thread_pool_info->exit_flag==0&&thread_pool_info->netfd_queue.size<=0){
            pthread_cond_wait(&thread_pool_info->cond,&thread_pool_info->mutex);
        }
        if(thread_pool_info->exit_flag==1){
            printf("the server is going to quit ,I as a thread have quited!!\n");
            pthread_mutex_unlock(&thread_pool_info->mutex);
            pthread_exit(NULL);
        }
        int old_netfd;
        int netfd=get_head_netfd(&thread_pool_info->netfd_queue,&old_netfd);
        printf("I as a thread got a netfd :%d\n",netfd);
        del_head_netfd(&thread_pool_info->netfd_queue);
        pthread_mutex_unlock(&thread_pool_info->mutex);

        // do work
        
        // getfile
        // putfile
        return NULL;
    }
}

//创建若干个工人线程
int create_worker(thread_pool_info_t *pthread_pool){                                                                                  
    for(int i=0;i<pthread_pool->worker_info.worker_num;i++){
        printf("id: %d thread has been created!\n",i+1);
        pthread_create(&pthread_pool->worker_info.worker_arr[i],NULL,thread_func,pthread_pool);
    }
    return 0;
}
